package com.learnpilot.datechange.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.learnpilot.datechange.service.CommandExecutor;
import com.learnpilot.datechange.service.OSServiceController;


public class WindowsServiceControllerImpl implements OSServiceController{

	private final Logger LOG = LoggerFactory.getLogger(WindowsServiceControllerImpl.class);
	private CommandExecutor commandExecutor;
	public void serviceStop(String serviceNmae) {
		getCommandExecutor().runCommand(prepareCommand("net stop "+serviceNmae ));
	}

	public void serviceStart(String serviceNmae) {
		getCommandExecutor().runCommand(prepareCommand("net start "+serviceNmae ));
	}

	public void serviceStartUpTypeChange(String serviceNmae, String option) {
		getCommandExecutor().runCommand(prepareCommand("sc config "+ serviceNmae +" start= "+option));
	}

	private String[] prepareCommand(String mainCommand){
		String[] command = new String[3];
		command[0] = "cmd";
		command[1] = "/c"; // "/C" command Executes the specified command and returns.
		command[2] = mainCommand;
		LOG.debug("Inside WindowsServiceControllerImpl.prepareCommand(). Command executed : "+command[2] );
		return command;
	}
	public CommandExecutor getCommandExecutor() {
		return commandExecutor;
	}

	public void setCommandExecutor(CommandExecutor commandExecutor) {
		this.commandExecutor = commandExecutor;
	}
}
